package client.gwt;

import com.google.gwt.user.client.ui.impl.RichTextAreaImplMozilla;
import com.google.gwt.user.client.DOM;
import client.ConfigHelper;

/**
 * Created by IntelliJ IDEA.
 * User: Wouter Wendelen
 * Date: 17-okt-2007
 * Time: 12:14:20
 */
public class MyRichTextAreaImplMozilla extends RichTextAreaImplMozilla {

    public void initElement() {
        initElement(ConfigHelper.getCssUrl(DOM.getElementAttribute(getElement(), "title")));
    }

    public native void initElement(String cssUrl) /*-{
    // Mozilla doesn't allow designMode to be set reliably until the iframe is
    // fully loaded.
    var _this = this;
    var iframe = _this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem;

    iframe.onload = function() {
      // Some Mozillae have the nasty habit of calling onload again when you set
      // designMode, so let's avoid doing it more than once.
      iframe.onload = null;

      // Send notification that the iframe has finished loading.
      _this.@com.google.gwt.user.client.ui.impl.RichTextAreaImplStandard::onElementInitialized()();

      // Don't set designMode until the RTA actually gets focused. This is
      // necessary because editing won't work on Mozilla if the iframe is
      // *hidden, but attached*. Waiting for focus gets around this issue.
      //
      // Note: This onfocus will not conflict with the addEventListener('focus',
      // ...) // in RichTextAreaImplStandard.
      iframe.contentWindow.onfocus = function() {
        iframe.contentWindow.onfocus = null;
		var styles = "@import url('" + cssUrl + "');";
		var newSS=document.createElement('link');
		newSS.rel='stylesheet';
		newSS.href='data:text/css,'+escape(styles);
		iframe.contentWindow.document.getElementsByTagName("head")[0].appendChild(newSS);
        iframe.contentWindow.document.designMode = 'On';
        iframe.contentWindow.document.addEventListener('contextmenu', function(e) { e.stopPropagation(); }, false);
      };
    };
  }-*/;
}
